# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: net-analyzer/netams/netams-4.0.1634.ebuild,v 1.0 2014/04/12 10:45:00 itspec.ru Exp $

EAPI="3"

inherit eutils user

DESCRIPTION="Network Traffic Accounting and Monitoring Software"
HOMEPAGE="http://www.netams.com"

SRC_URI="x86? ( http://www.netams.com/files/netams4/${PN}${PV}-linux-i386.tar.gz )
         amd64? ( http://www.netams.com/files/netams4/${PN}${PV}-linux-x64.tar.gz )
         x86-fbsd? ( http://www.netams.com/files/netams4/${PN}${PV}-freebsd-i386.tar.gz )
         amd64-fbsd? ( http://www.netams.com/files/netams4/${PN}${PV}-freebsd-x64.tar.gz )
         "
SLOT="4.0"
LICENSE="netams4-EULA"
RESTRICT="mirror"

KEYWORDS="~x86 ~amd64 ~x86-fbsd ~amd64-fbsd"

IUSE="agent pcap"

RDEPEND="
        !<dev-db/mysql-5.1
        agent? ( net-analyzer/n4agent )
        pcap? ( net-libs/libpcap )
        || (
                virtual/jre
                virtual/jdk
        )
"
S=${WORKDIR}

rdos2unix() {
    edos2unix $(find . -name '*'.$1 -type f) || die "Failed to convert line-endings of all .$1 files"
}

src_unpack() {
    unpack ${A}
}

src_prepare() {
    rm ${S}/startup.sh
    rm ${S}/jserver/jserver-startup.sh
    rm ${S}/jserver/bin/netams4.sh
    rm ${S}/radius/radius-startup.sh
    rm ${S}/repserver/repserver-startup.sh
    rm ${S}/webadmin/authserver-startup.sh
    rm ${S}/webadmin/webadmin-startup.sh
    rm ${S}/jserver/agent/n4agent-src.tar.gz
    rm ${S}/jserver/agent/n4agent_pcap
    rm ${S}/jserver/agent/n4agent_pcap-64
    rm ${S}/jserver/bin/n4ctl

    if use kernel_linux; then
        rm ${S}/jserver/backmysql-freebsd.sh
    else
        rm ${S}/jserver/backmysql.sh
    fi

    find ${S} -name '*.cmd' -type f -print0 | xargs -0 /bin/rm -f
    find ${S} -name '*.sh' -type f -print0 | xargs -0 sed -i 's:JAVA_HOME=../java:#JAVA_HOME=../java:'

    rdos2unix properties
    rdos2unix cfg

    mv ${S}/netams4.properties ${S}/netams4.properties.sample
    mv ${S}/jserver/db/string@ROOT.xml ${S}/jserver/db/string@ROOT.xml.sample
    mv ${S}/jserver/db/string@dhcp.xml ${S}/jserver/db/string@dhcp.xml.sample
    mv ${S}/jserver/db/defaultconditions.cfg ${S}/jserver/db/defaultconditions.cfg.sample
    mv ${S}/jserver/db/pbe.cfg ${S}/jserver/db/pbe.cfg.sample
    mv ${S}/jserver/db/prepaidvpn.cfg ${S}/jserver/db/prepaidvpn.cfg.sample
}

src_install() {
    into /opt
    dodir ${DESTTREE}/netams4 || die
    insinto ${DESTTREE}/netams4
    doins -r ${S}/*
    find ${D}/${DESTTREE}/netams4 -name '*.sh' -type f -print0 | xargs -0 chmod 0755
    chown -R netams4:netams4 ${D}/${DESTTREE}/netams4
    newinitd "${FILESDIR}"/netams4-authserver-init netams4-authserver
    newinitd "${FILESDIR}"/netams4-jserver-init netams4-jserver
    newinitd "${FILESDIR}"/netams4-radius-init netams4-radius
    newinitd "${FILESDIR}"/netams4-repserver-init netams4-repserver
    newinitd "${FILESDIR}"/netams4-webadmin-init netams4-webadmin
}

pkg_setup() {
    einfo "checking for necessary groups and users...  create if missing.\n"
    enewgroup netams4 || die "adding group netams4 failure"
    #enewuser netams4 -1 -1 /dev/null netams4 -c 'NeTAMS4' || die "adding user netams4 failure"
    #enewuser: extra arguments no longer supported;
    enewuser netams4 -1 -1 /dev/null netams4 || die "adding user netams4 failure"
}

pkg_postinst() {
    elog
    elog "For initial configuration please copy files"
    elog "\`${DESTTREE}/netams4/netams4.properties.sample\` to \`${DESTTREE}/netams4/netams4.properties\`"
    elog "\`${DESTTREE}/netams4/jserver/db/string@ROOT.xml.sample\` to \`${DESTTREE}/netams4/jserver/db/string@ROOT.xml\`"
    elog "\`${DESTTREE}/netams4/jserver/db/string@dhcp.xml.sample\` to \`${DESTTREE}/netams4/jserver/db/string@dhcp.xml\`"
    elog "\`${DESTTREE}/netams4/jserver/db/defaultconditions.cfg.sample\` to \`${DESTTREE}/netams4/jserver/db/defaultconditions.cfg\`"
    elog "\`${DESTTREE}/netams4/jserver/db/pbe.cfg.sample\` to \`${DESTTREE}/netams4/jserver/db/pbe.cfg\`"
    elog "\`${DESTTREE}/netams4/jserver/db/prepaidvpn.cfg.sample\` to \`${DESTTREE}/netams4/jserver/db/prepaidvpn.cfg\`"
    elog
}
